//
//  YXMainNavController.swift
//  SinaWeiBo
//
//  Created by Vergil on 2017/4/5.
//  Copyright © 2017年 Vergil. All rights reserved.
//

import UIKit

class YXMainNavController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器类型
            if let vc = viewController as? YXMainViewController  {
                
                var title = "返回"
                
                //如果是第一个返回标题显示上一个界面标题
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(backClick), isBack: true)
                
            }
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func backClick() {
        popViewController(animated: true)
    }

}
